<?php
namespace App\Models\Goods;

use Illuminate\Database\Eloquent\Model;

use App\Models\Goods\GoodsMarktingStrategy;

class GoodsSale extends Model
{

    protected $table = 'goods_sale';

    protected $primaryKey = 'postID';

    public $timestamps = false;

    public $incrementing = false;

    /**
     * 商品详情缓存信息
     * @param int $postID 商品网购号
     * @param int $shop_id 门店id
     * @return null|array
     */
    public static function getCache($postID, $shop_id = 0)
        
    {

        if( isset( $postID ) && !ebsig_is_int( $postID ) ) {
            return [];
        }

        $goods_sale = self::find($postID);
        if( !$goods_sale ) {
            return [];
        }
        $goods_sale = $goods_sale->toArray();

        if ($shop_id > 0) {
            $goods_markting_strategy = GoodsMarktingStrategy::where(['shop_id' => $shop_id,'postID' => $postID])->first();
            if ($goods_markting_strategy) {
                $goods_markting_strategy = $goods_markting_strategy->toArray();
                $goods_sale['marketPrice'] = $goods_markting_strategy['marketPrice'];
                $goods_sale['salePrice'] = $goods_markting_strategy['salePrice'];
                $goods_sale['salePoints'] = $goods_markting_strategy['salePoints'];
            }
        }

        return $goods_sale ;

    }

    /**
     * @param $barcode  条形码
     * @param int $shop_id  门店id
     * @return null|array
     */
    public static function getBarcodeCache($barcode, $shop_id = 0)

    {

        if (!isset( $barcode ) || empty( $barcode )) {
            return [];
        }

        $goods_sale = self::where(['barcode' => $barcode])->first();
        if (!$goods_sale) {
            return [];
        }
        $goods_sale = $goods_sale->toArray();

        if ($shop_id > 0) {
            $goods_markting_strategy = GoodsMarktingStrategy::where(['shop_id' => $shop_id,'postID' => $goods_sale['postID']])->first();
            if ( $goods_markting_strategy ) {
                $goods_markting_strategy = $goods_markting_strategy->toArray();
                $goods_sale['marketPrice'] = $goods_markting_strategy['marketPrice'];
                $goods_sale['salePrice'] = $goods_markting_strategy['salePrice'];
                $goods_sale['salePoints'] = $goods_markting_strategy['salePoints'];
            }
        }

        return $goods_sale ;
    }

}
